﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SurveyAdd.ascx.cs" company="hoandd">
//   Do Duc Hoan
// </copyright>
// <summary>
//   DesktopModules Surveys SurveyAdd
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DesktopModules.Surveys
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Localization;

    public partial class DesktopModules_Surveys_ListSurveys : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataToGridView();
            }
        }

        private void LoadDataToGridView()
        {
            List<SurveyInfo> lstSurvey = SurveyColtroller.GetSurveyss();
            grdSurvey.DataSource = lstSurvey;
            grdSurvey.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //Response.Redirect(EditUrl("[ADD_SURVEY]"));
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "ADD_SURVEY", "mid=" + ModuleId));
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (CheckGrid())
            {
                foreach (GridViewRow row in grdSurvey.Rows)
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                    if (chk != null)
                    {
                        if (chk.Checked)
                        {
                            HiddenField hdUserID = (HiddenField)row.FindControl("hdSurveyID");
                            if (hdUserID != null)
                            {
                                int surveyID = int.Parse(hdUserID.Value.ToString());
                                if (surveyID > 0)
                                {
                                    SurveyColtroller.DeleteSurveys(surveyID);
                                }
                            }
                        }
                    }
                }
                //Refresh Data
                LoadDataToGridView();
                divReport.Visible = true;
                lblReport.Text = "You have deleted successfully.";
            }
            else
            {
                divReport.Visible = true;
                lblReport.Text = "You have not chosen any exam to delete yet.";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(Globals.NavigateURL());
        }

        protected void grdSurvey_RowCreated(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
                }
            }
            catch (Exception ex)
            {
                //Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        protected void lbtPreviousGrdUser_Click(object sender, EventArgs e)
        {
        }

        protected void lbtNextGrdUser_Click(object sender, EventArgs e)
        {
        }

        protected void menuPagerGrdUser_MenuItemClick(object sender, MenuEventArgs e)
        {
        }

        protected void grdSurvey_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ((CheckBox)e.Row.FindControl("chkSelect1")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chkSelect1")).ClientID + "','" + grdSurvey.ClientID + "')");
            }
        }

        /// <summary>
        /// This is method check GridviewRow Select
        /// </summary>
        /// <returns></returns>
        private bool CheckGrid()
        {
            bool ok = false;
            foreach (GridViewRow row in grdSurvey.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        ok = true;
                        break;
                    }
                }
            }
            return ok;
        }

        protected void grdSurvey_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "editSurvey")
            {
                int surveyID = int.Parse(e.CommandArgument.ToString());
                if (surveyID > 0)
                {
                    Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "ADD_SURVEY", "mid=" + ModuleId, "surveyID=" + surveyID));
                }
            }
            if (e.CommandName == "DeleteSurvey")
            {
                int surveyID = int.Parse(e.CommandArgument.ToString());
                if (surveyID > 0)
                {
                    SurveyColtroller.DeleteSurveys(surveyID);
                    LoadDataToGridView();
                }
            }
        }
    }
}